package edu.sctu.yueding.activitys;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import edu.sctu.yueding.R;
import edu.sctu.yueding.bean.JsonBaseBeen;
import edu.sctu.yueding.bean.TokenResult;
import edu.sctu.yueding.config.CacheConfig;
import edu.sctu.yueding.config.URLConfig;
import edu.sctu.yueding.utils.JumpUtils;
import edu.sctu.yueding.utils.httputils.GetVolleyHttp;
import edu.sctu.yueding.utils.httputils.NetworkUtils;
import edu.sctu.yueding.utils.httputils.SharePreference;
import edu.sctu.yueding.view.TimeButton;

/**
 * Created by fll on 2016/6/6.
 */
public class WelcomeActivity extends BaseActivity {

    private SharePreference mSharePreferce;
    private Activity mActivity;
    private NetworkUtils networkUtils;
    private boolean isNetworkConnect;
    private SharePreference sharePreference;
    private ImageView welcomeImage;
    private TimeButton timeButton;
    private String username;
    private String password;


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        /**
//         * 去掉状态栏
//         */
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);


        setContentView(R.layout.activity_welcome);
        initSystemBarDDrawable(this, R.color.white);

        mActivity = WelcomeActivity.this;
        mSharePreferce = SharePreference.getInstance(mActivity);
        sharePreference = SharePreference.getInstance(this);
        welcomeImage = (ImageView) findViewById(R.id.welcome_image);
        timeButton = (TimeButton) findViewById(R.id.welcome_time);
        timeButton.setTextBefore("");
        timeButton.setLenght(3 * 1000);
        timeButton.setTextAfter("s");
        timeButton.setClick();

        welcomeImage.setImageResource(R.drawable.welcome);

        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                networkUtils = NetworkUtils.getIntance(WelcomeActivity.this);
                isNetworkConnect = networkUtils.isNetworkConnected();


                if (isNetworkConnect) {
                    //TODO:有网络连接
                } else {
                    //TODO:无网络连接
                }
                if (JumpUtils.isLogin(mActivity)) {

                    username = sharePreference.getString(CacheConfig.CACHE_USER_NAME);
                    password = sharePreference.getString(CacheConfig.CACHE_USER_PASSWORD);
                    sharePreference.setCache(CacheConfig.IS_EQUALS_USER, true);

                    HashMap<String, String> map = new HashMap<>();
                    map.put("login", username);
                    map.put("password", password);

                    GetVolleyHttp.getInstance(mActivity).getStringPOSTJsonData(URLConfig.LOGIN, map,
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {

                                    Log.e("refresh", response);


                                    try {
                                        com.alibaba.fastjson.JSONObject result = JSON.parseObject(response);

                                        if (result.getString("access_token") != null) {
                                            Log.e("refresh", result.getString("access_token"));
                                            sharePreference.setCache(CacheConfig.CACHE_TOKEN, result.getString("access_token"));
                                        }
                                    } catch (JSONException exception) {
                                        Toast.makeText(mActivity, "网络请求失败", Toast.LENGTH_LONG).show();
                                    }


                                }
                            }, new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {

                                    Toast.makeText(mActivity, "网络出了点小问题！", Toast.LENGTH_LONG).show();
                                }
                            }, CacheConfig.CACHE_INIT_TOKEN);

                    getChatTokenAndConnectRY(String.valueOf(sharePreference.getLong(CacheConfig.CACHE_USER_ID)));
                }


                JumpUtils.jumpActivity(WelcomeActivity.this, MainActivity.class);
                finish();

            }
        };

        timer.schedule(task, 3000);
    }
    private void getChatTokenAndConnectRY(String uesrId) {

        GetVolleyHttp.getInstance(this).getStringGETJsonData(URLConfig.getChatToken(uesrId), new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                JsonBaseBeen<TokenResult> t = JSON.parseObject(response, new TypeReference<JsonBaseBeen<TokenResult>>() {
                }.getType());

                if (t.getMeta().code == 200) {

                    Log.e("333333333", t.getData().getToken());
                    sharePreference.setCache(CacheConfig.CHAT_TOKEN,t.getData().getToken());
                    connect(t.getData().getToken());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        },"");

    }

}

